//创建参数配置通用控制器
for (var i = 1; i < menus[0].children.length; i++) {
  (function (page) {
    app.controller(page.path + 'Ctrl', function ($scope, $http, $location) {
      //页面通用初始化
      pageInit('module');
      //左侧导航用
      menuInit($scope, $http);
      $scope.menuIndex = page.menuIndex;

      //获取参数配置
      if (page.path == "rmm") {
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));
          //监视rmm部署模式
          var index = getIndexOfConfig($scope, 'rmm.mode', 'rmm');
          $scope.$watch('configs[' + index[0] + '].appList[' + index[1] + '].value', function (newValue, oldValue) {
            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));
            $scope.configs[index[0]].appList[index[1]].value = newValue;
            //动态改变配置项
            if (newValue == 1) {
              deleteConfigs($scope, ["rmm.timer.server", "deploy.event-node", "deploy.event-node|(event[R_TRD_REALTIME])", "deploy.event-node|(event[else])"]);
            } else if (newValue == 2) {
              deleteConfigs($scope, ["rmm.mode1.server", "deploy.event-node|(event[R_TRD_REALTIME])", "deploy.event-node|(event[else])"]);
            } else if (newValue == 3) {
              deleteConfigs($scope, ["rmm.mode1.server", "deploy.event-node"]);
            }
          }, true);
        });
      } else if (page.path == "cas") {

        //第三方免登录验证
        function changeThirdSsoEnable(temp) {
          if (temp.appList.length > 1) {
            var allFalse = true;
            for (var i in temp.appList) {
              if (temp.appList[i].value == "false") {
                var name = temp.appList[i].name;
                //动态删除配置项
                deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"], name);
              } else {
                allFalse = false;
              }
            }
            if (allFalse) {
              deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"]);
            }
          } else {
            if (temp.appList[0].value == "false") {
              deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"]);
            }
          }
        }

        //单点登录
        function changeCasEnabled(temp) {
          var allFalse = true;
          for (var i in temp.appList) {
            if (temp.appList[i].value == "true") {
              allFalse = false;
            }
          }
          if (allFalse) {
            deleteConfigs($scope, ["cas.server"]);
          }
        }

        //init回调
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));

          $scope.$watch(function () {
            //监视是否启用第三方免登录验证
            var index1 = getIndexOfConfig($scope, 'com.hundsun.uam.login.thirdSsoEnable');
            //监视是否启用单点登录
            var index2 = getIndexOfConfig($scope, 'client.casEnabled');
            return [$scope.configs[index1[0]], $scope.configs[index2[0]]];
          }, function (newValue, oldValue) {

            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));

            //第三方免登录验证
            var index1 = getIndexOfConfig($scope, 'com.hundsun.uam.login.thirdSsoEnable');
            $scope.configs[index1[0]] = newValue[0];
            changeThirdSsoEnable(newValue[0]);

            //单点登录
            var index2 = getIndexOfConfig($scope, 'client.casEnabled');
            $scope.configs[index2[0]] = newValue[1];
            changeCasEnabled(newValue[1]);

          }, true);
        });
      } else if (page.path == "msg") {
        configInit($scope, $http, page.path, $location, function () {
          //监视消息提醒开关
          var index1 = getIndexOfConfig($scope, 'enable_flag');
          //监视系统许可证过期提醒任务开关
          var index2 = getIndexOfConfig($scope, 'license_check_flag');
          $scope.$watch(function () {
            return [$scope.configs[index1[0]], $scope.configs[index2[0]]];
          }, function (newValue, oldValue) {

            //消息提醒开关
            // 数据初始化时校验互斥选项，如果同事选中，去除1.0
            if (oldValue[0].appList == newValue[0].appList) {
              if (newValue[0].appList.length > 1)
                if (newValue[0].appList[0].value == "1" && newValue[0].appList[1].value == "1")
                  $scope.configs[index1[0]].appList[0].value = "0";
            }
            $scope.configs[index1[0]] = newValue[0];
            if (newValue[0].appList.length > 1) {
              if (oldValue[0].appList[0].value == "0" && newValue[0].appList[0].value == "1")
                $scope.configs[index1[0]].appList[1].value = "0";
              if (oldValue[0].appList[1].value == "0" && newValue[0].appList[1].value == "1")
                $scope.configs[index1[0]].appList[0].value = "0";
            }

            //系统许可证过期提醒任务开关
            // 数据初始化时校验互斥选项，如果同事选中，去除1.0
            if (oldValue[1].appList == newValue[1].appList) {
              if (newValue[1].appList.length > 1)
                if (newValue[1].appList[0].value == "1" && newValue[1].appList[1].value == "1")
                  $scope.configs[index2[0]].appList[0].value = "0";
            }
            $scope.configs[index2[0]] = newValue[1];
            if (newValue[1].appList.length > 1) {
              if (oldValue[1].appList[0].value == "0" && newValue[1].appList[0].value == "1")
                $scope.configs[index2[0]].appList[1].value = "0";
              if (oldValue[1].appList[1].value == "0" && newValue[1].appList[1].value == "1")
                $scope.configs[index2[0]].appList[0].value = "0";
            }
          }, true);
        });
      } else if (page.path == "installmodel") {
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));
          //监视是否使用nginx
          var index1 = getIndexOfConfig($scope, 'nginx');
          var index2 = getIndexOfConfig($scope, 'nginx.apps');
          var index3 = getIndexOfConfig($scope, 'nginx.db.multi');
          var index4 = getIndexOfConfig($scope, 'nginx.db.current');
          $scope.$watch(function () {
            var arr = [];
            if (index1) arr.push($scope.configs[index1[0]]);
            if (index2) arr.push($scope.configs[index2[0]]);
            if (index3) arr.push($scope.configs[index3[0]]);
            if (index4) arr.push($scope.configs[index4[0]]);
            return arr;
          }, function (newValue, oldValue) {
            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));
            // 是否使用nginx统一入口
            $scope.configs[index1[0]] = newValue[0];
            // 统一入口的应用选择
            if (newValue[1]) {
              newValue[1].trueNum = 0;
              $scope.configs[index2[0]] = newValue[1];
              for (var ii = 0; ii < newValue[1].boxGroup.length; ii++)
                if (newValue[1].boxGroup[ii].value)
                  newValue[1].trueNum++;
            }
            // 是否是多数据库实例
            if (newValue[2]) {
              $scope.configs[index3[0]] = newValue[2];
            }
            // 当前数据库是否是主数据库
            if (newValue[3]) {
              $scope.configs[index4[0]] = newValue[3];
            }
            //动态改变配置项
            // 是否使用nginx统一入口
            if (newValue[0].value == "false") {
              deleteConfigs($scope, ["nginx.port", "nginx.apps", "nginx.db.multi", "nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 统一入口的应用选择
            if (newValue[1] && newValue[1].trueNum <= 1) {
              deleteConfigs($scope, ["nginx.db.multi", "nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 是否是多数据库实例
            if (newValue[2] && newValue[2].value == "false") {
              deleteConfigs($scope, ["nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 当前数据库是否是主数据库
            if (newValue[3] && newValue[3].value == "true") {
              deleteConfigs($scope, ["nginx.db.master.url", "nginx.db.master.password"]);
            }
          }, true);

        });
      } else if (page.path == "web") {
        configInit($scope, $http, page.path, $location, function () {

          $http.get('/deploy/param/installmodel/init').then(function (response) {
            if (response.status == 200) {
              var message = response.data;
              if (message.code == 0) {
                var installmodelConfigs = message.data;
                for (var j = 0; j < installmodelConfigs.length; j++) {
                  if (installmodelConfigs[j].name == "hadoop") {
                    var hide = !(installmodelConfigs[j].value == "true");
                  }
                }
                if (hide)
                  deleteConfigs($scope, ["hadoop.server"]);
              } else {
                $.showNotice(message.msg, "danger");
              }
            }
          }).catch(function (response) {
            $.showNotice(response.status + " error：" + response.data.message, "danger");
          });

        });
      } else
        configInit($scope, $http, page.path, $location);
    });
  })(menus[0].children[i]);
}
//单实例数据库配置 database.html
app.controller('databaseCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('database');
  //初始化
  $http.get('/deploy/db/init').then(function (response) {
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var data = message.data;
        $scope.bean = data;
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });

  //测试数据库
  $scope.dbTest = function (bean) {
    $('#database .progress').slideDown(200);
    $('#database .alert-success').slideUp(200);
    $http.get('/deploy/db/test').then(function (response) {
      $('#database .progress').slideUp(200);
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#database .alert-success').slideDown(200);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //配置数据库
  $scope.dbConfig = function (bean) {
    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.get('/deploy/db/submit').then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $location.path('version');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

});
//多实例数据库配置 database-multi.html
app.controller('databaseMultiCtrl', function ($scope, $http) {
  //页面通用初始化
  pageInit('database');
  //初始化
  $http.get('/deploy/main/init').then(function (response) {
    $scope.apps = apps;
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var data = message.data;
        //单选框
        $scope.databaseSchema = data.databaseSchema;
        //多选框
        var deployApps = data.deployApps ? data.deployApps : [];
        for (var i = 0; i < deployApps.length; i++) {
          apps[deployApps[i]].sel = true;
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });
  $scope.apps = apps;
  $scope.changeTab = function (index) {
    $("#database-tab" + index).parent().addClass("active").siblings().removeClass("active");
    $("#database-tab" + index + "-content").addClass("active").siblings().removeClass("active");
  }
});
//参数配置-数据库配置 dbset.html
app.controller('dbsetCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('dbset');
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 2.1;
  init();
  //获取/刷新用户列表
  function init() {
    $http.get('/deploy/dbset/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var data = message.data;
          //设置默认值
          for (var i in data.databases)
            if (!data.databases[i].password)
              data.databases[i].password = data.databases[i].username;

          $scope.data = data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //检查密码
  $scope.checkPWD = function (db) {
    if (!db.password || Trim(db.password, true) == '') {
      db.isNull = true;
    } else {
      db.isNull = false;
    }
  };

  //新增用户
  $scope.addFnc = function (add) {
    //其他参数
    if (!add) {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!add.username || add.username == "") {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!add.password || add.password == "") {
      $.showNotice("密码不能为空", "danger");
      return;
    }
    if (!add.passwordC || add.passwordC == "") {
      $.showNotice("确认密码不能为空", "danger");
      return;
    }
    if (add.password != add.passwordC) {
      $.showNotice("两次输入密码不相同", "danger");
      return;
    }

    add.ip = $scope.data.ip;
    add.port = $scope.data.port;
    add.sid = $scope.data.sid;

    $http.post('/deploy/dbset/add', add).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#exampleModal').modal('hide');
          $.showNotice("添加成功");
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //删除用户
  $scope.delFnc = function (del) {
    $http.post('/deploy/dbset/del', del).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice("删除成功");
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //测试
  $scope.testFnc = function (bean) {
    $('#dbset .progress').slideDown(200);
    $('#dbset .alert-success').slideUp(200);
    $http.post('/deploy/dbset/test', bean).then(function (response) {
      if (response.status == 200) {
        $('#dbset .progress').slideUp(200);
        var message = response.data;
        if (message.code == 0) {
          $('#dbset .alert-success').slideDown(200);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //上一步
  $scope.dbsetGoBack = function () {
    $location.path(checkThisLast(1.12).path);
  };

  //下一步
  $scope.submitFnc = function (bean) {
    //检查表单
    for (var i in $scope.data.databases) {
      if ($scope.data.databases[i].isNull) {
        $.showNotice('请完成表单表单填写，密码不允许为空', 'warning');
        return;
      }
    }

    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.post('/deploy/dbset/submit', bean).then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          // 脱机版
          if (isOffline) {
            $location.path('platform');
            return;
          }
          $location.path('installDB');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

});
app.controller('doneCtrl', function ($scope, $http, $location) {
  pageInit(-1);
  $("#nav-bar").scope().isFix = true;
});
//错误 error.html
app.controller('errorCtrl', function () {
  $("#path-bar").scope().pageIndex = -1;
});
app.controller('helpCtrl', function ($scope, $http, $location) {
  pageInit(-1);
  $("#nav-bar").scope().isFix = true;
});
//数据安装-数据库脚本安装 installDB.html
app.controller('installDBCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('dbset');
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 2.2;

  init();
  $scope.isContinue = false;

  //执行日志全屏
  $scope.logFull = function (isLogFull) {
    if (!isLogFull)
      $(document.body).css("overflow-y", "hidden");
    else
      $(document.body).css("overflow-y", "auto");
    $scope.isLogFull = !isLogFull;
  }

  //折叠功能
  $scope.collapse = [];
  $scope.collapseFnc = function (i) {
    $scope.collapse[i] = $scope.collapse[i] ? false : true;
  };
  $scope.exAll = function () {
    for (var i in $scope.sqlListPkgs)
      $scope.collapse[i] = true;
  };

  $scope.$watch('installing', function () {
    $("#path-bar").scope().installing = $scope.installing;
  }, true);

  //改变备份状态
  $scope.changeBackup = function () {
    $http.post('/deploy/install/db/backup', {
      backup: $scope.isBackup
    }).then(function (response) {
      if (response.status == 200) {
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //获取/刷新接口类型和接口列表
  function init() {

    $http.get('/deploy/install/db/backup').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.listBackup = message.data.users;
          $scope.isBackupShow = message.data.show;
          $scope.isBackup = message.data.backup || !message.data.show;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });

    $http.get('/deploy/install/db/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.sqlListPkgs = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //改变正在安装的sql
  function changeInstall(installingPkg, installingSql) {
    //正在安装的sql
    $scope.installingPkg = installingPkg;
    $scope.installingSql = installingSql;
  }

  //开始安装
  function startInstall(installingPkg, installingSql, isOneSql, isOnePackage) {
    if (!$scope.isContinue) {
      //清空日志
      $scope.runLogs = [];
      p = 0;
    }
    $("#installDB-backup").slideUp();
    $("#installDB-log").slideDown();
    //置为正在安装状态
    $scope.installing = true;
    //正在安装的sql
    changeInstall(installingPkg, installingSql);
    //开始轮询日志
    if (isOneSql) {
      realLog();
      $scope.interfaceInterval = setInterval(function () {
        realLog();
      }, 2000)
    } else if (isOnePackage) {
      realLog();
      schedule(installingPkg);
      $scope.interfaceInterval = setInterval(function () {
        realLog();
        schedule(installingPkg);
      }, 2000)
    } else {
      realLog();
      schedule();
      $scope.interfaceInterval = setInterval(function () {
        realLog();
        schedule();
      }, 2000)
    }
  }

  //结束安装
  function stopInstall() {
    $scope.installing = false;
    $scope.isContinue = false;
    $scope.installingPkg = "";
    $scope.installingSql = "";

    //结束轮询
    clearInterval($scope.interfaceInterval);

    realLog();
    init();
  }

  //暂停安装
  function pauseInstall() {
    //结束轮询
    clearInterval($scope.interfaceInterval);
  }

  //继续安装
  function continueInstall() {
    $scope.isContinue = true;
    if ($scope.continueType.isAll) {
      //一键安装
      $scope.oneKey();
    } else if ($scope.continueType.isPackage) {
      //单个包安装
      $scope.onePackage($scope.continueType.index);
    } else {
      //单个SQL安装
      $scope.oneSql($scope.continueType.packageName, $scope.continueType.sqlName);
    }
  }

  //实时日志查询
  function schedule(installingPkg) {
    var temp = installingPkg ? ("/package?packageName=" + installingPkg) : "";
    $http.get('/deploy/install/db/schedule' + temp).then(function (response) {
      var message = response.data.data;
      $scope.sqlListPkgs = message.interfaces;
      changeInstall(message.targetPackage.name, message.targetSql.name);
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //实时日志查询
  var p = 0;

  function realLog() {
    $http.post('/deploy/install/db/real/log', {
      pos: p
    }).then(function (response) {
      var message = response.data;
      if (message.code == 0) {
        p = message.data.pos;
        //更新日志数据
        $scope.runLogs = $scope.runLogs.concat(message.data.log);

        //判断日志的滚动条是否在底部
        var all = $("#installDB-log .console")[0].scrollHeight;
        var top = $("#installDB-log .console")[0].scrollTop;
        var height = $("#installDB-log .console").height();
        var flag = (top + height + 19 <= all || top + height + 21 >= all);
        //如果之前日志的滚动条在底部则保持在底部
        if (flag)
          setTimeout(function () {
            $("#installDB-log .console")[0].scrollTop = $("#installDB-log .console")[0].scrollHeight;
          }, 0);
      } else {
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //改变checkbox
  $scope.changeCheckbox = function (isPkg) {
    if (isPkg)
      for (var i in $scope.sqlListPkgs)
        for (var j in $scope.sqlListPkgs[i].sqlList)
          $scope.sqlListPkgs[i].sqlList[j].checked = $scope.sqlListPkgs[i].checked;
    else
      for (var i in $scope.sqlListPkgs) {
        var flag = true;
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if (!$scope.sqlListPkgs[i].sqlList[j].checked) {
            flag = false;
            break;
          }
        }
        $scope.sqlListPkgs[i].checked = flag;
      }
  };

  //一键安装
  $scope.oneKey = function (isAll) {
    $scope.exAll();
    //如果是安装所有则全部置为未安装状态
    if (isAll)
      for (var i in $scope.sqlListPkgs) {
        $scope.sqlListPkgs[i].checked = false;
        for (var j in $scope.sqlListPkgs[i].sqlList)
          $scope.sqlListPkgs[i].sqlList[j].checked = false;
      }

    $http.post('/deploy/install/db/oneKeySetup', $scope.sqlListPkgs).then(function (response) {

      var message = response.data;
      if (message.code == 0) {
        stopInstall();
        $.showNotice(message.msg);
      } else if (message.code == 1) {
        pauseInstall();
        $('#installDB-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isAll: true
        }
      } else if (message.code == 2) {
        pauseInstall();
        $('#installDB-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isAll: true
        }
      } else {
        stopInstall();
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall();
  };

  //单独安装
  $scope.onePackage = function (index) {
    //展开
    $scope.collapse[index] = true;
    $http.post('/deploy/install/db/onePackage', $scope.sqlListPkgs[index]).then(function (response) {
      var message = response.data;
      if (message.code == 0) {
        stopInstall();
        if ($scope.sqlListPkgs[index].checked)
          $.showNotice("无需安装，如需重新安装请先取消【已全部安装】勾选", "danger");
        else
          $.showNotice(message.msg);
      } else if (message.code == 1) {
        pauseInstall();
        $('#installDB-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 2) {
        pauseInstall();
        $('#installDB-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 3) {
        pauseInstall();
        $('#installDB-version').modal('show');
        $scope.errSql = message.data;
        $scope.isPlatform = false;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 4) {
        pauseInstall();
        $('#installDB-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.isPlatform = true;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else {
        stopInstall();
        $.showNotice(message.msg);
      }

    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall($scope.sqlListPkgs[index].name, "", false, true);
  };

  //单独安装
  $scope.oneSql = function (packageName, sqlName) {
    $http.post('/deploy/install/db/oneSql', {
      packageName: packageName,
      sqlName: sqlName
    }).then(function (response) {
      stopInstall();
      var message = response.data;
      if (message.code == 0) {
        $.showNotice(message.msg);
      } else if (message.code == 1) {
        pauseInstall();
        $('#installDB-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: false,
          packageName: packageName,
          sqlName: sqlName
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall(packageName, sqlName, true);
  };

  //查看日志
  $scope.getLog = function (packageName, sqlName) {
    $http.post('/deploy/install/db/log', {
      packageName: packageName,
      sqlName: sqlName
    }).then(function (response) {
      if (response.data.code == 0) {
        $('#installDB-console').modal('show');
        $scope.logs = response.data.data;
      }
      else
        $.showNotice(response.data.msg, "danger");
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //权限验证
  $scope.setManager = function (manager) {
    if (!manager || !Trim(manager.manager, true)) {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!manager.password || !Trim(manager.password, true)) {
      $.showNotice("密码不能为空", "danger");
      return;
    }
    $scope.setManagerFlag = true;
    $http.post('/deploy/install/db/set/manager', manager).then(function (response) {
      if (response.status == 200) {
        $scope.setManagerFlag = false;
        var message = response.data;
        if (message.code == 0) {
          $('#installDB-manger').modal('hide');
          $.showNotice(message.msg, "info");
          continueInstall();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.setManagerFlag = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  //停止安装
  $scope.stopManager = stopInstall;
  // 已手动执行
  $scope.yesManager = function () {
    $('#installDB-manger').modal('hide');
    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    if ($scope.continueType.isPackage)
      continueInstall();
    else {
    }
  }

  //确认备份并继续安装
  $scope.confirmYes = function () {
    $('#installDB-confirm').modal('hide');

    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    continueInstall();
  };
  // 重新备份并继续安装
  $scope.confirmBackup = function () {
    // 删除旧备份并重新备份
    $http.post('/deploy/install/db/delete/backup', {
      packageName: $scope.errSql.packageName
    }).then(function (response) {
      if (response.status == 200) {
        $scope.setManagerFlag = false;
        var message = response.data;
        if (message.code == 0) {
          $('#installDB-confirm').modal('hide');
          continueInstall();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  //没有备份并停止安装
  $scope.confirmNo = stopInstall;

  //确认版本并继续安装
  $scope.versionYes = function () {
    $('#installDB-version').modal('hide');

    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    continueInstall();
  };
  //没有备份并停止安装
  $scope.versionNo = stopInstall;

  //查看编译失效对象
  $scope.$watch('sqlListPkgs', function () {
    $scope.compileFlag = $scope.sqlListPkgs && $scope.sqlListPkgs.length > 0 ? true : false;
    for (var i in $scope.sqlListPkgs) {
      for (var j in $scope.sqlListPkgs[i].sqlList) {
        if (!$scope.sqlListPkgs[i].sqlList[j].checked)
          $scope.compileFlag = false;
      }
    }
  }, true);

  function getCompileItems() {
    $http.get('/deploy/install/db/compile?pageNo=' + ($scope.nowPage - 1) + '&pageSize=10').then(function (response) {
      var message = response.data;
      $scope.pages = [];
      for (var i = 0; i < message.data.pages; i++)
        $scope.pages.push(i + 1);
      $scope.compileItems = message.data.items;
      $scope.compileItemsTotal = message.data.total;
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  $scope.compileDialog = function () {
    $('#installDB-compile-dialog').modal('show');
    //分页
    $scope.nowPage = 1;
    getCompileItems();

    //切换分页并处理分页位置
    var cal = 8;
    $scope.calPage1 = 0;
    $scope.calPage2 = cal - $scope.calPage1;
    $scope.changePage = function (page) {
      if (page > 0 && page < $scope.pages.length + 1) {
        $scope.nowPage = page;
        getCompileItems();
        if (page <= cal / 2 + 1) {
          $scope.calPage1 = page - 1;
          $scope.calPage2 = cal - page + 1;
        } else if (page >= $scope.pages.length - cal / 2) {
          $scope.calPage2 = $scope.pages.length - page;
          $scope.calPage1 = cal + page - $scope.pages.length;
        } else {
          $scope.calPage1 = cal / 2;
          $scope.calPage2 = cal / 2;
        }
      }
    };

  };

  //导入安装包
  $scope.importInstall = function () {
    $http.get('/deploy/install/db/importPackages/1').then(function (response) {
      var rowData = response.data.data;
      var data = [];
      for (var i in rowData) {
        var isNew = true;
        for (var j in $scope.sqlListPkgs) {
          if (rowData[i].substr(0, rowData[i].lastIndexOf(".")) == $scope.sqlListPkgs[j].name) {
            isNew = false;
          }
        }
        if (isNew) data.push(rowData[i]);
      }
      $scope.importPkgs = data;
      $('#installDB-import-dialog').modal('show');
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  $scope.addImport = function (name) {
    $scope.importing = true;
    $http.post('/deploy/install/db/import', {
      fileName: name
    }).then(function (response) {
      $scope.importing = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice(message.msg, "success");
          $('#installDB-import-dialog').modal('hide');
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.importing = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //删除安装包
  $scope.delPkg = function (name) {
    if (confirm("确认删除：" + name)) {
      $http.post('/deploy/install/db/del/package', {
        name: name
      }).then(function (response) {
        if (response.status == 200) {
          var message = response.data;
          if (message.code == 0) {
            $.showNotice(message.msg, "success");
            init();
          } else {
            $.showNotice(message.msg, "danger");
          }
        }
      }).catch(function (response) {
        $.showNotice(response.status + " error：" + response.data.message, "danger");
      });
    }
  };

  //清空安装包
  $scope.delPkgs = function () {
    if (confirm("确认清空所有安装包？")) {
      $http.post('/deploy/install/db/del/packages').then(function (response) {
        if (response.status == 200) {
          var message = response.data;
          if (message.code == 0) {
            $.showNotice(message.msg, "success");
            init();
          } else {
            $.showNotice(message.msg, "danger");
          }
        }
      }).catch(function (response) {
        $.showNotice(response.status + " error：" + response.data.message, "danger");
      });
    }
  };

  //顺序调整
  function changeOrder(data) {
    console.log(data);

    $http.post('/deploy/install/db/swap', data).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });

  }

  $scope.up = function (index) {
    if (index <= 0) return;
    var postData = {};
    postData.prev = index == 1 ? "" : $scope.sqlListPkgs[index - 2].name;
    postData.target = $scope.sqlListPkgs[index].name;
    postData.next = $scope.sqlListPkgs[index - 1].name;

    changeOrder(postData);
  };

  $scope.down = function (index) {
    if (index >= $scope.sqlListPkgs.length - 1) return;
    var postData = {};
    postData.prev = $scope.sqlListPkgs[index + 1].name;
    postData.target = $scope.sqlListPkgs[index].name;
    postData.next = index == $scope.sqlListPkgs.length - 2 ? "" : $scope.sqlListPkgs[index + 2].name;

    changeOrder(postData);
  };

  //提交
  $scope.submitFnc = function () {

    if ($scope.loadingNext) return;
    $scope.loadingNext = true;

    $http.post('/deploy/install/db/submit').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          //init();
          $location.path('installInterface');
        } else {
          $scope.loadingNext = false;
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

});
//接口包脚本安装 installInterface.html
app.controller('installInterfaceCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('interface');
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 3.2;

  init();
  $scope.isContinue = false;

  //执行日志全屏
  $scope.logFull = function (isLogFull) {
    if (!isLogFull)
      $(document.body).css("overflow-y", "hidden");
    else
      $(document.body).css("overflow-y", "auto");
    $scope.isLogFull = !isLogFull;
  }
  //折叠功能
  $scope.collapse = [];
  $scope.collapseFnc = function (i) {
    $scope.collapse[i] = $scope.collapse[i] ? false : true;
  };
  $scope.exAll = function () {
    for (var i in $scope.sqlListPkgs)
      $scope.collapse[i] = true;
  };

  //
  $scope.$watch('installing', function () {
    $("#path-bar").scope().installing = $scope.installing;
  }, true);

  //获取/刷新接口类型和接口列表
  function init() {
    $scope.hasTips = false;
    $http.get('/deploy/install/interface/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          if (message.data.errorMsg)
            $.showNotice(message.data.errorMsg, "danger");
          $scope.sqlListPkgs = message.data.packages;
          for (var i = 0; i < $scope.sqlListPkgs.length; i++) {
            for (var j = 0; j < $scope.sqlListPkgs[i].sqlList.length; j++) {
              var name = $scope.sqlListPkgs[i].sqlList[j].name.toLowerCase();
              if (name.lastIndexOf('r.sql') > -1 || name.lastIndexOf('r_view.sql') > -1) {
                $scope.hasTips = true;
                break;
              }
            }
          }
          setTimeout(function () {
            $('[data-toggle="tooltip"]').tooltip();
          }, 0);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //改变正在安装的sql
  function changeInstall(installingPkg, installingSql) {
    //正在安装的sql
    $scope.installingPkg = installingPkg;
    $scope.installingSql = installingSql;
  }

  //开始安装
  function startInstall(installingPkg, installingSql, isOneSql, isOnePackage) {
    if (!$scope.isContinue) {
      //清空日志
      $scope.runLogs = [];
      p = 0;
    }
    $("#installInterface-log").slideDown();
    //置为正在安装状态
    $scope.installing = true;
    //正在安装的sql
    changeInstall(installingPkg, installingSql);
    //开始轮询日志
    if (isOneSql) {
      realLog();
      $scope.interfaceInterval = setInterval(function () {
        realLog();
      }, 2000)
    } else if (isOnePackage) {
      realLog();
      schedule(installingPkg);
      $scope.interfaceInterval = setInterval(function () {
        realLog();
        schedule(installingPkg);
      }, 2000)
    } else {
      realLog();
      schedule();
      $scope.interfaceInterval = setInterval(function () {
        realLog();
        schedule();
      }, 2000)
    }
  }

  //结束安装
  function stopInstall() {
    $scope.installing = false;
    $scope.isContinue = false;
    $scope.installingPkg = "";
    $scope.installingSql = "";

    //结束轮询
    clearInterval($scope.interfaceInterval);

    realLog();
    init();
  }

  //暂停安装
  function pauseInstall() {
    //结束轮询
    clearInterval($scope.interfaceInterval);
  }

  //继续安装
  function continueInstall() {
    $scope.isContinue = true;
    if ($scope.continueType.isAll) {
      //一键安装
      $scope.oneKey();
    } else if ($scope.continueType.isPackage) {
      //单个包安装
      $scope.onePackage($scope.continueType.index);
    } else {
      //单个安装
      $scope.oneSql($scope.continueType.packageName, $scope.continueType.sqlName);
    }
  }

  //实时日志查询
  function schedule(installingPkg) {
    var temp = installingPkg ? ("/package?packageName=" + installingPkg) : "";
    $http.get('/deploy/install/interface/schedule' + temp).then(function (response) {
      var message = response.data.data;
      $scope.sqlListPkgs = message.interfaces;
      changeInstall(message.targetPackage.name, message.targetSql.name);
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //实时日志查询
  var p = 0;

  function realLog() {
    $http.post('/deploy/install/interface/real/log', {
      pos: p
    }).then(function (response) {
      var message = response.data;
      if (message.code == 0) {
        p = message.data.pos;
        //更新日志数据
        $scope.runLogs = $scope.runLogs.concat(message.data.log);

        //判断日志的滚动条是否在底部
        var all = $("#installInterface-log .console")[0].scrollHeight;
        var top = $("#installInterface-log .console")[0].scrollTop;
        var height = $("#installInterface-log .console").height();
        var flag = (top + height + 19 <= all || top + height + 21 >= all);
        //如果之前日志的滚动条在底部则保持在底部
        if (flag)
          setTimeout(function () {
            $("#installInterface-log .console")[0].scrollTop = $("#installInterface-log .console")[0].scrollHeight;
          }, 0);
      } else {
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //查看注意事项
  function getReadme(version) {
    function isArray(o) {
      return Object.prototype.toString.call(o) == '[object Array]';
    }

    var postData = isArray(version) ? version : new Array(version);
    $('#installInterface-notice').modal('show');
    $http.post('/deploy/install/interface/readme', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.readme = message.data[0].remark;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //改变checkbox
  $scope.changeCheckbox = function (isPkg) {
    if (isPkg)
      for (var i in $scope.sqlListPkgs)
        for (var j in $scope.sqlListPkgs[i].sqlList)
          $scope.sqlListPkgs[i].sqlList[j].checked = $scope.sqlListPkgs[i].checked;
    else
      for (var i in $scope.sqlListPkgs) {
        var flag = true;
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if (!$scope.sqlListPkgs[i].sqlList[j].checked) {
            flag = false;
            break;
          }
        }
        $scope.sqlListPkgs[i].checked = flag;
      }
  };

  //一键安装
  $scope.oneKey = function (isAll) {
    $scope.exAll();

    //如果是安装所有则全部置为未安装状态
    if (isAll)
      for (var i in $scope.sqlListPkgs) {
        $scope.sqlListPkgs[i].checked = false;
        for (var j in $scope.sqlListPkgs[i].sqlList)
          $scope.sqlListPkgs[i].sqlList[j].checked = false;
      }

    $http.post('/deploy/install/interface/oneKeySetup', $scope.sqlListPkgs).then(function (response) {

      var message = response.data;
      if (message.code == 0) {
        stopInstall();
        $.showNotice(message.msg);
        //注意事项
        var version = [];
        for (var i in $scope.sqlListPkgs)
          version.push($scope.sqlListPkgs[i].version);
        getReadme(version);
      } else if (message.code == 1) {
        pauseInstall();
        $('#installInterface-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isAll: true
        }
      } else if (message.code == 2) {
        pauseInstall();
        $('#installInterface-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isAll: true
        }
      } else {
        stopInstall();
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall();
  };

  //单独安装
  $scope.onePackage = function (index) {
    //展开
    $scope.collapse[index] = true;

    $http.post('/deploy/install/interface/onePackage', $scope.sqlListPkgs[index]).then(function (response) {
      var message = response.data;
      if (message.code == 0) {
        stopInstall();
        if ($scope.sqlListPkgs[index].checked)
          $.showNotice("无需安装，如需重新安装请先取消【已全部安装】勾选", "danger");
        else
          $.showNotice(message.msg);
      } else if (message.code == 1) {
        pauseInstall();
        $('#installInterface-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 2) {
        pauseInstall();
        $('#installInterface-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 3) {
        pauseInstall();
        $('#installInterface-version').modal('show');
        $scope.errSql = message.data;
        $scope.isPlatform = false;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else if (message.code == 4) {
        pauseInstall();
        $('#installInterface-confirm').modal('show');
        $scope.errSql = message.data;
        $scope.isPlatform = true;
        $scope.continueType = {
          isPackage: true,
          index: index
        }
      } else {
        stopInstall();
        $.showNotice(message.msg);
      }

    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall($scope.sqlListPkgs[index].name, "", false, true);
  };

  //单独安装
  $scope.oneSql = function (packageName, sqlName, tips) {
    $http.post('/deploy/install/interface/oneSql', {
      packageName: packageName,
      sqlName: sqlName
    }).then(function (response) {
      stopInstall();
      var message = response.data;
      if (message.code == 0) {
        $.showNotice(message.msg);
        //判断是否需要弹出注意事项
        $http.get('/deploy/install/interface/init').then(function (response) {
          if (response.status == 200) {
            var message = response.data;
            if (message.code == 0) {
              for (var i in message.data)
                if (message.data[i].name == packageName && message.data[i].checked == true) {
                  var version = message.data[i].version;
                  //注意事项
                  getReadme(version);
                }
            } else {
              $.showNotice(message.msg, "danger");
            }
          }
        }).catch(function (response) {
          $.showNotice(response.status + " error：" + response.data.message, "danger");
        });
      } else if (message.code == 1) {
        pauseInstall();
        $('#installInterface-manger').modal('show');
        $scope.errSql = message.data;
        $scope.continueType = {
          isPackage: false,
          packageName: packageName,
          sqlName: sqlName
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }).catch(function (response) {
      stopInstall();
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    startInstall(packageName, sqlName, true);
  };

  //查看日志
  $scope.getLog = function (packageName, sqlName, tips) {
    $http.post('/deploy/install/interface/log', {
      packageName: packageName,
      sqlName: sqlName
    }).then(function (response) {
      if (response.data.code == 0) {
        $('#installInterface-console').modal('show');
        $scope.logs = response.data.data;
      } else
        $.showNotice(response.data.msg, "danger");
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //权限验证
  $scope.setManager = function (manager) {
    if (!manager || !Trim(manager.manager, true)) {
      $.showNotice("用户名不能为空", "danger");
      return;
    }
    if (!manager.password || !Trim(manager.password, true)) {
      $.showNotice("密码不能为空", "danger");
      return;
    }
    $scope.setManagerFlag = true;
    $http.post('/deploy/install/interface/set/manager', manager).then(function (response) {
      if (response.status == 200) {
        $scope.setManagerFlag = false;
        var message = response.data;
        if (message.code == 0) {
          $('#installInterface-manger').modal('hide');
          $.showNotice(message.msg, "info");
          continueInstall();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.setManagerFlag = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  //停止安装
  $scope.stopManager = stopInstall;
  // 已手动执行
  $scope.yesManager = function () {
    $('#installInterface-manger').modal('hide');
    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    if ($scope.continueType.isPackage)
      continueInstall();
    else { }
  }

  //确认备份并继续安装
  $scope.confirmYes = function () {
    $('#installInterface-confirm').modal('hide');
    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    continueInstall();
  };
  // 重新备份并继续安装
  $scope.confirmBackup = function () {
    // 删除旧备份并重新备份
    $http.post('/deploy/install/interface/delete/backup', {
      packageName: $scope.errSql.packageName
    }).then(function (response) {
      if (response.status == 200) {
        $scope.setManagerFlag = false;
        var message = response.data;
        if (message.code == 0) {
          $('#installInterface-confirm').modal('hide');
          continueInstall();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  // 没有备份并停止安装
  $scope.confirmNo = stopInstall;

  // 确认版本并继续安装
  $scope.versionYes = function () {
    $('#installInterface-version').modal('hide');

    //跳过错误sql
    for (var i in $scope.sqlListPkgs) {
      if ($scope.sqlListPkgs[i].name == $scope.errSql.packageName) {
        for (var j in $scope.sqlListPkgs[i].sqlList) {
          if ($scope.sqlListPkgs[i].sqlList[j].name == $scope.errSql.sqlName)
            $scope.sqlListPkgs[i].sqlList[j].checked = true;
        }
      }
    }
    continueInstall();
  };
  //没有备份并停止安装
  $scope.versionNo = stopInstall;

  //提交
  $scope.submitFnc = function () {
    $location.path('platform');
    /*if ($scope.loadingNext) return;
    $scope.loadingNext = true;

    $http.post('/deploy/install/interface/submit').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $location.path('platform');
        } else {
          $scope.loadingNext = false;
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });*/
  };

  //导入安装包
  $scope.importInstall = function () {
    $http.get('/deploy/install/db/importPackages/2').then(function (response) {
      var rowData = response.data.data;
      var data = [];
      for (var i in rowData) {
        var isNew = true;
        for (var j in $scope.sqlListPkgs) {
          if (rowData[i].substr(0, rowData[i].lastIndexOf(".")) == $scope.sqlListPkgs[j].name) {
            isNew = false;
          }
        }
        if (isNew) data.push(rowData[i]);
      }
      $scope.importPkgs = data;
      $('#installInterface-import-dialog').modal('show');
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  $scope.addPkgs = [];
  $scope.addImports = function (pkg) {
    var index = $scope.addPkgs.indexOf(pkg);
    if (index == -1) {
      $scope.addPkgs.push(pkg);
    } else {
      $scope.addPkgs.splice(index, 1);
    }
  }

  $scope.addImport = function () {
    $scope.importing = true;
    $http.post('/deploy/install/interface/import', {
      fileName: $scope.addPkgs
    }).then(function (response) {
      $scope.importing = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice(message.msg, "success");
          $('#installInterface-import-dialog').modal('hide');
          init();
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.importing = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //删除安装包
  $scope.delPkg = function (name) {
    if (confirm("确认删除：" + name)) {
      $http.post('/deploy/install/interface/del/package', {
        name: name
      }).then(function (response) {
        if (response.status == 200) {
          var message = response.data;
          if (message.code == 0) {
            $.showNotice(message.msg, "success");
            init();
          } else {
            $.showNotice(message.msg, "danger");
          }
        }
      }).catch(function (response) {
        $.showNotice(response.status + " error：" + response.data.message, "danger");
      });
    }
  };

  //清空安装包
  $scope.delPkgs = function () {
    if (confirm("确认清空所有安装包？")) {
      $http.post('/deploy/install/interface/del/packages').then(function (response) {
        if (response.status == 200) {
          var message = response.data;
          if (message.code == 0) {
            $.showNotice(message.msg, "success");
            init();
          } else {
            $.showNotice(message.msg, "danger");
          }
        }
      }).catch(function (response) {
        $.showNotice(response.status + " error：" + response.data.message, "danger");
      });
    }
  };


  //查看编译失效对象
  $scope.$watch('sqlListPkgs', function () {
    $scope.compileFlag = $scope.sqlListPkgs && $scope.sqlListPkgs.length > 0 ? true : false;
    for (var i in $scope.sqlListPkgs) {
      for (var j in $scope.sqlListPkgs[i].sqlList) {
        if (!$scope.sqlListPkgs[i].sqlList[j].checked)
          $scope.compileFlag = false;
      }
    }
  }, true);

  function getCompileItems() {
    $http.get('/deploy/install/interface/compile?pageNo=' + ($scope.nowPage - 1) + '&pageSize=10').then(function (response) {
      var message = response.data;
      $scope.pages = [];
      for (var i = 0; i < message.data.pages; i++)
        $scope.pages.push(i + 1);
      $scope.compileItems = message.data.items;
      $scope.compileItemsTotal = message.data.total;
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  $scope.compileDialog = function () {
    $('#installInterface-compile-dialog').modal('show');
    //分页
    $scope.nowPage = 1;
    getCompileItems();

    //切换分页并处理分页位置
    var cal = 8;
    $scope.calPage1 = 0;
    $scope.calPage2 = cal - $scope.calPage1;
    $scope.changePage = function (page) {
      if (page > 0 && page < $scope.pages.length + 1) {
        $scope.nowPage = page;
        getCompileItems();
        if (page <= cal / 2 + 1) {
          $scope.calPage1 = page - 1;
          $scope.calPage2 = cal - page + 1;
        } else if (page >= $scope.pages.length - cal / 2) {
          $scope.calPage2 = $scope.pages.length - page;
          $scope.calPage1 = cal + page - $scope.pages.length;
        } else {
          $scope.calPage1 = cal / 2;
          $scope.calPage2 = cal / 2;
        }
      }
    };

  };

});
//接口包配置 interface.html
app.controller('interfaceCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('interface');
  // 脱机
  $scope.isOffline = isOffline;
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 3.1;

  init();

  function dataHandle(rowData) {
    var interfaceTypeListIndex = [{
      index: 1,
      id: 'FSK',
      name: '全面风险',
      include: []
    }, {
      index: 2,
      id: 'CRK',
      name: '信用评级',
      include: []
    }, {
      index: 3,
      id: 'NK',
      name: '内控4.0',
      include: []
    }];

    for (var i in rowData) {
      var interfaceTypeName = rowData[i].interfaceTypeName;
      for (var j in rowData[i].infoList) {
        var productId = parseInt(rowData[i].infoList[j].productId);
        var include = interfaceTypeListIndex[productId - 1].include;
        if (include.indexOf(parseInt(i)) == -1) {
          include.push(parseInt(i));
        }
      }
    }

    return interfaceTypeListIndex;
  }

  //获取/刷新接口类型和接口列表
  function init() {
    $http.get('/deploy/interface/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var rowData = message.data
          $scope.interfaceTypeList = rowData;
          $scope.interfaceTypeListIndex = dataHandle(rowData);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //获取/刷新接口类型列表
  function initTypes() {
    $http.get('/deploy/interface/add').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.addTypeList = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //模块新增接口类别
  $('#interface-dialog').on('show.bs.modal', initTypes);

  $scope.addFnc = function (addTypeList) {
    var types = [];
    for (var i = 0; i < addTypeList.length; i++)
      if (addTypeList[i].checked) types.push(addTypeList[i].interfaceTypeId);
    if (types.length == 0) {
      $.showNotice("请正确选择接口类别", "danger");
      return;
    }
    var postData = {
      types: types
    };

    //执行添加
    $http.post('/deploy/interface/add', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          init();
          initTypes();
          $.showNotice("操作成功", "success");
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });

  };

  // 改变状态即提交
  $scope.checkedChange = function () {
    var rowData = $scope.interfaceTypeList;
    var postData = [];
    for (var i = 0; i < rowData.length; i++) {
      for (var j = 0; j < rowData[i].infoList.length; j++) {
        if (rowData[i].infoList[j].checked)
          postData.push(rowData[i].infoList[j].interfaceVersion);
      }
    }

    $http.post('/deploy/interface/submit', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          // 操作成功
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 提交
  $scope.submitFnc = function () {

    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    var rowData = $scope.interfaceTypeList;

    var postData = [];
    for (var i = 0; i < rowData.length; i++) {
      for (var j = 0; j < rowData[i].infoList.length; j++) {
        if (rowData[i].infoList[j].checked)
          postData.push(rowData[i].infoList[j].interfaceVersion);
      }
    }

    $http.post('/deploy/interface/submit', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          //init();
          // 脱机版
          if (isOffline) {
            $location.path('platform');
            return;
          }
          $location.path('installInterface');
        } else {
          $scope.loadingNext = false;
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

});
//首页 main.html
app.controller('mainCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('main');
  //初始化
  $http.get('/deploy/main/init').then(function (response) {
    $scope.apps = apps;
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var data = message.data.bean;
        //单选框
        $scope.databaseSchema = data.databaseSchema;
        //多选框
        var deployApps = data.deployApps ? data.deployApps : [];
        for (var i = 0; i < deployApps.length; i++) {
          apps[deployApps[i]].sel = true;
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });
  //方法
  $scope.radio = function (index) {
    $scope.databaseSchema = index;
  };
  // 调度1.0和2.0不能被同时选中的处理
  /* $scope.checkbox = function (index) {
    if (index == 0 && apps[1].sel == true) {
      apps[1].sel = false;
    } else if (index == 1 && apps[0].sel == true) {
      apps[0].sel = false;
    }
  }; */
  //提交
  $scope.deploy = function (dispatcher) {

    //处理参数
    var deployApps = [];
    for (var i = 0; i < apps.length; i++) {
      if (apps[i].sel == true) deployApps.push(i);
    }

    if (!dispatcher) {
      // 全部不勾选
      if (!apps[0].sel && !apps[1].sel) {
        $.showNotice("[调度1.0] 和 [调度2.0] 至少需要勾选一个！", "danger");
        return;
      }
      // 全部勾选
      if (apps[0].sel && apps[1].sel) {
        $('#main-confirm').modal('show');
        return;
      }
      deploy(-1);
    } else {
      $('#main-confirm').modal('hide');
      $('#main-confirm').on('hidden.bs.modal', function (e) {
        deploy(dispatcher);
      })
    }

    //部署
    function deploy(dispatcher) {
      if ($scope.loadingNext) return;
      $scope.loadingNext = true;
      $http.post('/deploy/main/submit', {
        databaseSchema: $scope.databaseSchema,
        deployApps: deployApps,
        dispatcher: dispatcher
      }).then(function (response) {
        $scope.loadingNext = false;
        if (response.status == 200) {
          var message = response.data;
          if (message.code == 0) {
            var data = message.data;
            if ($scope.databaseSchema == 0)
              $location.path('database');
            if ($scope.databaseSchema == 1)
              $location.path('database-multi');
          } else {
            $.showNotice(message.msg, "danger");
          }
        }
      }).catch(function (response) {
        $scope.loadingNext = false;
        $.showNotice(response.status + " error：" + response.data.message, "danger");
      });
    }

  };
});
app.controller('md5Ctrl', function ($scope, $http, $location) {
  pageInit(-1);
  $("#nav-bar").scope().isFix = true;

  $scope.apps = {
    'nk': '内控4.0',
    'fsk': "全面风险",
    'crk': "信用评级"
  };

  $http.get('/md5/init').then(function (response) {
    if (response.status == 200) {
      var data = response.data;
      if (data.code == 0) {
        $scope.allApp = data.data;
        $scope.nowAppKey = 'nk';
        $scope.nowApp = $scope.allApp['nk'];
      } else {
        $.showNotice(data.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });

  $scope.radio = function (key) {
    $scope.nowAppKey = key;
    $scope.nowApp = $scope.allApp[key];
  }

  $scope.pageItemNum = 15;
  $scope.pageItemNumArr = [];
  for (var i = 0; i < $scope.pageItemNum; i++)
    $scope.pageItemNumArr.push(i);

  function getCompileItems(users) {
    $scope.loading = true;
    $http.post('/md5/execute', {
      users: users
    }).then(function (response) {
      $scope.loading = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#md5-dialog').modal('show');

          $scope.compileItems = message.data;
          $scope.compileItemsTotal = message.data.length;

          var pageNum = $scope.compileItemsTotal % $scope.pageItemNum == 0 ? $scope.compileItemsTotal / $scope.pageItemNum - 1 : $scope.compileItemsTotal / $scope.pageItemNum;

          $scope.pages = [];
          for (var i = 0; i < pageNum; i++)
            $scope.pages.push(i + 1);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loading = false;
      $.showNotice(response.status + " error：" + response.data.msg, "danger");
    });
  }

  $scope.compileDialog = function (users) {
    //分页
    $scope.nowPage = 1;
    getCompileItems(users);

    //切换分页并处理分页位置
    var cal = 8;
    $scope.calPage1 = 0;
    $scope.calPage2 = cal - $scope.calPage1;
    $scope.changePage = function (page) {
      if (page > 0 && page < $scope.pages.length + 1) {
        $scope.nowPage = page;
        if (page <= cal / 2 + 1) {
          $scope.calPage1 = page - 1;
          $scope.calPage2 = cal - page + 1;
        } else if (page >= $scope.pages.length - cal / 2) {
          $scope.calPage2 = $scope.pages.length - page;
          $scope.calPage1 = cal + page - $scope.pages.length;
        } else {
          $scope.calPage1 = cal / 2;
          $scope.calPage2 = cal / 2;
        }
      }
    };

  };

  $scope.content = "加载中..."

  $scope.fullContent = function (text) {
    $scope.content = text;
  }

});
//参数配置-当前模块 module.html
app.controller('moduleCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('module');
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 1.1;
  init();
  //获取/刷新模块列表
  function init() {
    $http.get('/deploy/module/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.modules = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //下一步
  $scope.submitFnc = function () {
    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.get('/deploy/module/submit').then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $location.path('installmodel');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
});
//顶栏
app.controller('navCtrl', function ($scope, $http) {
  // 脱机
  // isOffline = true;
  $http.get('/deploy/main/offline').then(function (response) {
    isOffline = response.data.data.offline;

    var s = "";
    if (isOffline) s = " - 脱机版"
    $scope.systemName = systemName + s;
    $scope.systemVersion = systemVersion;
    $scope.systemMsgs = [];
    $scope.hasNew = 0;
  });

  $('.msg-dropdown').click(function (e) {
    e.stopPropagation();
  });
});

//路径导航
app.controller('pathCtrl', function ($scope, $http) {

  $scope.pageIndex = 0;
  $http.get('/deploy/main/offline').then(function (response) {
    isOffline = response.data.data.offline;

    if (isOffline) $scope.pages = pagesOffline;
    else $scope.pages = pages;
  });

});
// 平台部署
app.controller('platformCtrl', function ($scope, $http, $location, $timeout) {
  //页面通用初始化
  pageInit('platform');
  // 脱机
  $scope.isOffline = isOffline;
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 4.1;

  // 是否包含其他必须操作的按钮，依次为【多数据源数据比对】
  $scope.hasSpecialBtn = {
    "/deploy/db/multi": 0
  }
  // 判断是否存在需要执行的其他按钮
  $scope.checkSpecialBtn = function () {
    for (var key in $scope.hasSpecialBtn)
      if ($scope.hasSpecialBtn[key] == 1)
        return true;
    return false;
  }

  // 是否包含需要提示操作的按钮，依次为【导入内控报表】【导入全面风险报表】
  $scope.hasSpecialBtn2 = {
    "/deploy/report/management": 0,
    "/deploy/report/fsk": 0
  }
  // 判断是否存在需要提示操作的其他按钮
  $scope.checkSpecialBtn2 = function () {
    for (var key in $scope.hasSpecialBtn2)
      if ($scope.hasSpecialBtn[key] == 1)
        return true;
    return false;
  }


  // 阻止刷新
  // $(window).bind('beforeunload', function () {
  //   if ($scope.hasChange)
  //     return '离开该页面，手动更改的配置都将还原，确认离开？';
  // });
  init();

  //按照应用分类
  function handleRowData(rowData) {
    var data = {};
    for (var key in rowData) {
      var type = key.split("/")[0];
      if (!data[type]) {
        data[type] = {};
      }
      data[type][key] = rowData[key];
    }
    return data;
  }

  function init() {
    $scope.hasChange = true;
    //初始化预览
    $http.get('/deploy/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.types = handleRowData(message.data);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    //初始化按钮
    $http.get('/deploy/button').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.btns = message.data;
          for (var i = 0; i < $scope.btns.length; i++) {
            // 将必要执行按钮标记为未执行
            for (var key in $scope.hasSpecialBtn) {
              if ($scope.btns[i].url == key)
                $scope.hasSpecialBtn[key] = 1;
            }
            // 将提示操作按钮标记为未执行
            for (var key in $scope.hasSpecialBtn2) {
              if ($scope.btns[i].url == key)
                $scope.hasSpecialBtn2[key] = 1;
            }
          }
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 平台部署
  $scope.deploy = function () {
    $scope.isDeploy = true;
    $http.get('/deploy/start').then(function (response) {
      $scope.isDeploy = false;
      if (response.status == 200) {
        var message = response.data;
        $scope.hasChange = false;
        if (message.code == 0) {
          // $location.path('done');
          $scope.hasChange = false;
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.isDeploy = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  // 数据源替换
  $scope.replace = function () {
    $scope.isReplace = true;
    $http.get('/deploy/replace').then(function (response) {
      $scope.isReplace = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#platform-console').modal('show');
          $scope.logs = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.isReplace = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  // 手动修改
  $scope.editConfig = function (type, fileName, key, value) {
    $('#platform-edit').modal('show');
    $scope.nowType = type;
    $scope.nowFileName = fileName;
    $scope.nowKey = key;
    $scope.nowValue = value;
  }

  // 执行修改
  $scope.doChange = function () {
    $http.post('/deploy/modify', {
      path: $scope.nowFileName,
      key: $scope.nowKey,
      value: $scope.nowValue
    }).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.types[$scope.nowType][$scope.nowFileName][$scope.nowKey] = $scope.nowValue;
          $scope.hasChange = true;
          $('#platform-edit').modal('hide');
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 其他操作
  $scope.doSomething = function (url) {
    $http.get(url).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $.showNotice(message.msg);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
    // 将必要执行按钮标记为已执行
    for (var key in $scope.hasSpecialBtn) {
      if (url == key)
        $scope.hasSpecialBtn[key] = 2;
    }
    // 将提示操作按钮标记为已执行
    for (var key in $scope.hasSpecialBtn2) {
      if (url == key)
        $scope.hasSpecialBtn2[key] = 2;
    }
  };


  // 控制跳转
  $scope.confirmGo = function (path) {
    // 忽略修改
    $scope.justGo = function () {
      $('#platform-confirm').modal('hide');
      $scope.hasChange = false;
      // alert(path);
      $scope.confirmGo(path);
    }
    // 忽略提示按钮的操作
    $scope.justGo2 = function (id) {
      var key = '/deploy/report/' + id;
      $('#platform-' + id).modal('hide');
      $scope.hasSpecialBtn2[key] = 2;
      $scope.confirmGo(path);
    }

    // 平台部署
    if ($scope.hasChange) {
      $('#platform-confirm').modal('show');
    }
    else {
      // 提示操作按钮
      for (var key in $scope.hasSpecialBtn2) {
        if ($scope.hasSpecialBtn2[key] == 1) {
          var id = key.substr(key.lastIndexOf('/') + 1);
          $('#platform-' + id).modal('show');
          return;
        }
      }
      // 跳转
      $location.path(path);
      return;
    }
  }

});
//版本 version.html
app.controller('versionCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('version');
  // 脱机
  $scope.isOffline = isOffline;
  //发送数据
  var sendData = { versions: [] };
  //初始化
  $http.get('/deploy/version/init').then(function (response) {
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var data = message.data;
        $scope.data = data;
        //初始化发送数据格式
        for (var i = 0; i < data.versions.length; i++) {
          var version = {};
          version.index = data.versions[i].index;
          version.installVersions = data[version.index] ? data[version.index] : 0;
          sendData.versions.push(version);
        }
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });
  //选择升级版本
  $scope.change = function (index, installVersion) {
    for (var i = 0; i < sendData.versions.length; i++)
      if (sendData.versions[i].index == index) sendData.versions[i].installVersions = installVersion;
    //得到平台版本
    $http.post('/deploy/version/common', { 'versions': sendData.versions }).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var data = message.data;
          $scope.platformVersion = data.installVersions;
          //修改平台版本 index==5
          for (var i = 0; i < sendData.versions.length; i++)
            if (sendData.versions[i].index == 5) sendData.versions[i].installVersions = data.installVersions;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };
  //只有平台的情况下
  $scope.changeOnly = function (installVersion) {
    sendData.versions[0].installVersions = installVersion;
  };
  //提交
  $scope.submit = function (implementer) {
    //验证版本错误情况
    if (sendData.versions.length == 0) {
      $.showNotice("出现错误，无法进行下一步操作", "danger");
      return;
    }
    //验证实施人员
    if (!implementer) {
      $.showNotice("实施人员不能为空", "danger");
      return;
    }
    //验证版本选择
    for (var i = 0; i < sendData.versions.length; i++)
      if (sendData.versions[i].installVersions == 0) {
        $.showNotice("请正确选择版本，保证所有产品版本正确", "danger");
        return;
      }
    //提交
    sendData.implementer = implementer;
    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    $http.post('/deploy/version/submit', sendData).then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $location.path('module');
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }
});